#!/bin/bash
# 
# desc:
#     单元测试
# author:
#     dockerlin@163.com
# 

oneTimeSetUp(){
    echo "version is ${SHUNIT_VERSION}"
    . ./index
    testResultFile=`mktemp testresultXXXXXX.txt`
    logger_info "开始单元测试"
}

oneTimeTearDown(){
    # oneTimeTearDown只执行一次
    if [ ${just_once} ]; then return 0; fi
    just_once=1
    rm ${testResultFile}
    unset testResultFile
    logger_info "--测试报告--"
    #echo "用例是否成功0成功1失败 :: ${__shunit_testSuccess}"
    logger_info "测试用例总数 :: ${__shunit_testsTotal}"
    logger_info "通过用例数 :: ${__shunit_testsPassed}"
    logger_info "失败用例数 :: ${__shunit_testsFailed}"
    logger_info "总功能点 :: ${__shunit_assertsTotal}"
    logger_info "通过功能点 :: ${__shunit_assertsPassed}"
    logger_info "失败功能点 :: ${__shunit_assertsFailed}"
    logger_info "跳过功能点 :: ${__shunit_assertsSkipped}" 
    logger_info "----------"
}

testCxlibAwk(){
    testStr="abcdefindmeg"
    searchStr1="findme"
    searchStr2="notfind"
    strpos=`cxlib_awk_strpos ${testStr} ${searchStr1}`
    ${_ASSERT_TRUE_} '" should find ${searchStr1} in ${testStr} "' '"[ ${strpos} -gt 0 ]"'
    ${_ASSERT_EQUALS_} '"should find ${searchStr1} in ${testStr} at position 6 "'  6 ${strpos}
    strpos=`cxlib_awk_strpos ${testStr} ${searchStr2}`
    ${_ASSERT_EQUALS_} "在${testStr}找到字符串${searchStr2}"  0 ${strpos}
}

testCxlibOpenssl(){
    # 测试rand函数
    randStr1=$(cxlib_openssl_rand "base64" 10)
    #echo "randStr1=${randStr1}"
    randStr2=$(cxlib_openssl_rand "hex" 10)
    #echo "randStr2=${randStr2}"
    randStr3=$(cxlib_openssl_rand)
    #echo "randStr3=${randStr3}"
    randStr3Length=${#randStr3}
    randStr1Lastchar=${randStr1:0-1}
    #echo $randStr1Lastchar
    ${_ASSERT_NOT_EQUALS_} '"rand not equal ${randStr2} and  ${randStr3} "'  '"${randStr3}"' '"${randStr2}"'
    ${_ASSERT_EQUALS_} '"rand str ${randStr2} length should be 20 "'  20 "${randStr3Length}"
    ${_ASSERT_EQUALS_} '"rand str ${randStr1} should be end with = "'  '"="' "${randStr1Lastchar}"
    # 测试摘要算法
    md5Str=$(cxlib_openssl_digest "123456" "md5")
    ${_ASSERT_EQUALS_} '"123456 should be e10adc3949ba59abbe56e057f20f883e after digest md5"'  "e10adc3949ba59abbe56e057f20f883e" "${md5Str}"
    sha1Str=$(cxlib_openssl_digest "123456" "sha1")
    ${_ASSERT_EQUALS_} '"123456 should be 7c4a8d09ca3762af61e59520943dc26494f8941b after digest sha1"'  "7c4a8d09ca3762af61e59520943dc26494f8941b" "${sha1Str}"
    md5Str1=$(cxlib_openssl_md5 "123456")
    ${_ASSERT_EQUALS_} '"123456 should be e10adc3949ba59abbe56e057f20f883e after digest md5"'  "e10adc3949ba59abbe56e057f20f883e" "${md5Str1}"
    sha1Str1=$(cxlib_openssl_sha1 "123456")
    ${_ASSERT_EQUALS_} '"123456 should be 7c4a8d09ca3762af61e59520943dc26494f8941b after digest sha1"'  "7c4a8d09ca3762af61e59520943dc26494f8941b" "${sha1Str1}"
    # 测试加解密算法
    base_string='我们是害虫，正义的来福灵快来把我杀死！'
    encrypt_key='226a89e66d0dcc79c9673150fa176001'
    encrypt_algorithm='aes-128-ecb'
    encrypt_string=$(cxlib_openssl_encrypt "${base_string}" "${encrypt_key}" "${encrypt_algorithm}" "hex")
    decrypt_string=$(cxlib_openssl_decrypt "${encrypt_string}" "${encrypt_key}" "${encrypt_algorithm}" "hex")
    ${_ASSERT_EQUALS_} '"base_string should be equal decrypt_string with hex"'  "${base_string}" "${decrypt_string}"
    encrypt_string=$(cxlib_openssl_encrypt "${base_string}" "${encrypt_key}" "${encrypt_algorithm}" "base64")
    decrypt_string=$(cxlib_openssl_decrypt "${encrypt_string}" "${encrypt_key}" "${encrypt_algorithm}" "base64")
    #echo ${encrypt_string}
    #echo ""
    #echo ${decrypt_string}
    #echo ""
    ${_ASSERT_EQUALS_} '"base_string should be equal decrypt_string with base64"'  "${base_string}" "${decrypt_string}"
}

. ./test/shunit2